package com.apache.ius.controller;

import com.apache.api.api.ApiService;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.client.UctCoreClient;
import com.apache.client.UctProtoclClient;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.datasource.ConnctionData;
import com.apache.ius.able.ExportDataTask;
import com.apache.ius.able.ScheduledExecutor;
import com.apache.ius.common.CommonUtils;
import com.apache.ius.common.annotion.BeanFactory;
import com.apache.ius.common.connectors.impl.AdapterFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.*;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.PBOSSOTools;
import com.apache.uct.common.ToolsUtil;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * description:获取对象信息公共接口实现Action
 */
public class SuperTransAction extends BaseAction {

    /**
     * 统一入口方法
     */
    @RequestMapping(value = {"/trans/info/","/trans/list/"}, method = RequestMethod.GET)
    @ResponseBody
    public Object doInvoke(HttpServletRequest request,HttpServletResponse response)  throws Exception {
        String doCode = StrUtil.doNull(request.getParameter("doCode"), "dymicSql");
        String uri = request.getRequestURI().replaceAll(request.getContextPath(),"");
        Method method = doInvoke(this.getClass(), request, response, uri, doCode);
        try {
            if (null != method) {
                Object obj = method.invoke(this, request, response);
                return obj;
            } else {
                gotoErrorPage(request, response, "请求的Action地址未定义");
            }
        }catch (Exception e){
            log.info("执行失败(doInvoke)：", e);
            ResultMsg msg = new ResultMsg("F", "请求失败，请重试");
            JSONObject array = JSONObject.fromObject(msg);
           return array;
        }
        return null;
    }

    /**
     * 统一入口方法
     */
    @RequestMapping(value = "/trans/submit/", method = RequestMethod.POST)
    @ResponseBody
    public void submitInvoke(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String doCode = StrUtil.doNull(request.getParameter("doCode"), "dymicSql");
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        if (ToolsUtil.isEmpty(loginUser)) {
            gotoLogin(request, response, "");
            return;
        }
        String uri = request.getRequestURI().replaceAll(request.getContextPath(),"");
        Method method = doInvoke(this.getClass(), request, response, uri, doCode);
        try {
            if (null != method) {
                Object obj = method.invoke(this, request, response);
                if (!ToolsUtil.isEmpty(obj)) {
                    JSONObject array = JSONObject.fromObject(obj);
                    request.setAttribute(BeanFactory.RETURN_MSG_KEY,array.toString());
                    this.outputJson(array.toString(),response,"");
                }
            } else {
                gotoErrorPage(request, response, "请求的Action地址未定义");
            }
        }catch (Exception e){
            log.info("执行失败(doInvoke)：", e);
            ResultMsg msg = new ResultMsg("F", "请求失败，请重试");
            JSONObject array = JSONObject.fromObject(msg);
            request.setAttribute(BeanFactory.RETURN_MSG_KEY,array.toString());
        }
    }

    /**
     * 查询标的列表
     */
    @RequestMapping(value = "/trans/info/dymicSql", method = RequestMethod.GET)
    @ResponseBody
    public Object infoDymicSql(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        params.put("resultType", StrUtil.doNull(request.getParameter("resultType"), "objInfo"));
        params.put("resultObjType", "obj");
        params = AdapterFactory.getInstance()
                .getReqParamForBefor("setReqParam", params, request, response);//前拦截
        String execptionMsg = String.valueOf(params.get("execptionMsg"));
        if (StrUtil.isNotNull(execptionMsg)) {
            ResultMsg msg = new ResultMsg("F", execptionMsg);
            JSONObject array = JSONObject.fromObject(msg);
            return array;
        }
        ParamsVo vo = new ParamsVo();
        vo.setParams(params);
        vo.setMethodKey("dymicSql");
        ResultEntity entity = doIusService(vo);//.doInvoke(request, response,
                //params);
        JSONObject array =null;
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
            array = JSONObject.fromObject(new ResultMsg("F", entity.getMessage()));
        } else {
            if (entity.getEntity() instanceof String) {
                ResultMsg msg = new ResultMsg("F", entity.getMessage());
                array = JSONObject.fromObject(msg);
            } else {
                array = JSONObject.fromObject(entity.getEntity());
            }
        }
        return array;
    }

    /**
     * 查询标的列表
     */
    @RequestMapping(value = "/trans/list/dymicSql", method = RequestMethod.GET)
    @ResponseBody
    public Object listDymicSql(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);

        String modelTypes = params.get("modelTypes");
        //如果不传modelTypes，还是沿用以前自己写sql的方式，否则走ius的公共方法。
        params.put("resultType", StrUtil.doNull(request.getParameter("resultType"), "objInfo"));
        params.put("resultObjType", "list");
        params = AdapterFactory.getInstance()
                .getReqParamForBefor("setReqParam", params, request, response);//前拦截
        String execptionMsg = String.valueOf(params.get("execptionMsg"));
        if (StrUtil.isNotNull(execptionMsg)) {
            ResultMsg msg = new ResultMsg("F", execptionMsg);
            JSONObject array = JSONObject.fromObject(msg);
            return array;
        }
        ParamsVo vo = new ParamsVo();
        vo.setParams(params);
        vo.setMethodKey("dymicSql");
        //        String cacheKey = oscache.getCacheKey("dymicSql", params);
        // ResultEntity entity = (ResultEntity) oscache.get(cacheKey);
        // if (ToolsUtil.isEmpty(entity)) {
        ResultEntity entity = doIusService(vo);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            //            if (null != entity.getEntity()) {
            //                oscache.put(cacheKey, entity);
            //            }
            if ("RelationByProId".equals(request.getParameter("sqlKeyId"))) {
                resultMap.put("dealFlag", "T");//需特殊处理
            }
        }
        getListData(entity, resultMap);
        //JSONObject array = JSONObject.fromObject(resultMap);
        return resultMap;
    }

    /**
     * 根据需求修改任何表的字段
     */
    @RequestMapping(value = "/trans/submit/dymicSql", method = RequestMethod.POST)
    @ResponseBody
    public Object submitDymicSql(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        params.put("resultType", "processSql");
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        Map<String, Object> result = new HashMap<String, Object>();
        params.put("loginUser_userEname", loginUser.getUserEname());//登录人昵称
        params.put("loginUser_userId",loginUser.getUserId());//登录人id
        params = AdapterFactory.getInstance()
                .getReqParamForBefor("setReqParam,createInfo", params, request, response);//前拦截
        String execptionMsg = String.valueOf(params.get("execptionMsg"));
        if (StrUtil.isNotNull(execptionMsg)) {
            ResultMsg msg = new ResultMsg("F", execptionMsg);
            return msg;
        }
        ParamsVo vo = new ParamsVo();
        vo.setParams(params);
        vo.setMethodKey("dymicSql");
        ResultEntity entity = doIusService(vo);
//        result.put("flag", String.valueOf(entity.getResult()));
//        if (entity.getEntity() instanceof String) {
//            if ("F".equals(String.valueOf(entity.getEntity())) || "T"
//                    .equals(String.valueOf(entity.getEntity()))) {
//                result.put("flag", entity.getEntity());
//            }
//            result.put("ids", entity.getEntity());
//        }else if(entity.getEntity() instanceof HashMap){
//            Map map = (Map)entity.getEntity();
//            if(map==null || map.isEmpty()){
//                result.put("flag", entity.getEntity());
//            }
//            result.put("ids", map.values());
//        }
//        result.put("msg", String.valueOf(entity.getMessage()));
        result = super.getResultEntityData(entity);
        return result;
    }

    /**
     * 数据导出
     */
    @RequestMapping(value = "/trans/export/data", method = RequestMethod.GET)
    @ResponseBody
    public Object exportData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        String thisSysName = CommonUtils.getSysEname();
        log.info("-");
        log.info("export导出处理开始....");
        ParamsVo vo = new ParamsVo();
        vo.setMethodKey("dymicSql");
        vo.setParams(params);
        String exportFileName = null;
        String nowDateStrs = DateUtils.format(new Date(),"yyyyMMdd");
        if(StrUtil.isNull(params.get("pageName")) && StrUtil.isNull(params.get("formName"))){
            //走自定义sql(必要参数：数据表名+所属系统)
            vo.setParams("exportType","unIus");
            exportFileName = StrUtil.doNull(params.get("fileName"), params.get("tableName"));
        } else {
            //走ius接口pageName,formName
            vo.setParams("resultType", StrUtil.doNull(request.getParameter("resultType"), "objInfo"));
            vo.setParams("resultObjType", "list");
            vo.setParams(AdapterFactory.getInstance().getReqParamForBefor("setReqParam", vo.getParams(), request, response));//前拦截
            exportFileName = StrUtil.doNull(params.get("fileName"), vo.getParamsToStr("exportFileName"));
            exportFileName = StrUtil.doNull(exportFileName, thisSysName);
        }
        String fileType = "."+StrUtil.doNull(request.getParameter("fileType"),"xlsx");
        exportFileName+="_"+nowDateStrs;
        vo.setParams("exportFileName", exportFileName);
        exportFileName += UUIDUtil.getRandomStr("1", 4, 97)+fileType;//补充文件后缀
        try {
            ExportDataTask exportTask = new ExportDataTask();
            Map<String,Object> resqData = exportTask.exportConfig(request.getParameter("pageName"),request.getParameter("formName"));
            String execptionMsg = String.valueOf(params.get("execptionMsg"));
            if (StrUtil.isNotNull(execptionMsg)) {
                return new ResultMsg("F", execptionMsg);
            }
            ApiService apiService = null;
            if(null == this.iusService) {
                Object obj = SpringContextLoader.getBean("iusService");
                if (obj != null) {
                    apiService = (ApiService) obj;
                }
            } else {
                apiService = iusService;
            }
            String filePath = ConfigUtil.getInstance().getLocalByKey("config.properties","UPLOAD_DIRECTORY");
            filePath += (filePath.endsWith("/") ? "" : "/")+"export";
            resqData.put("filePath",filePath);
            resqData.put("fileName",exportFileName);
            resqData.put("fileType",fileType);
            FileOperate.getInstance().newCreateFolder(filePath);
            exportTask.setParams(vo, apiService,resqData);
            Future<Integer> future1 = ScheduledExecutor.instance().submit(exportTask);
            if (future1.get() == 0) {
                return new ResultMsg("F", String.valueOf(resqData.get("ex_msg")));
            }else{
                if(resqData.containsKey("ex_msg")){
                    return new ResultMsg("F", String.valueOf(resqData.get("ex_msg")));
                }
            }
        } catch (Exception ex) {
            return new ResultMsg("F", "导出出错："+ex.getMessage());
        }
        String downUrl = ConfigUtil.getInstance().getLocalByKey("config.properties","DOWNLOAD_URL")+"/export/"+exportFileName;
        Map<String,String> resMap = new HashMap<>();
        resMap.put("flag","T");
        resMap.put("msg","导出完成");
        resMap.put("downUrl",downUrl);
        resMap.put("fileName",exportFileName);
        return resMap;
    }

    /**
     * 测试数据源是否可正常连接
     */
    @RequestMapping(value = "/trans/info/testDbConnection", method = RequestMethod.POST)
    @ResponseBody
    public Object testDbConnection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        Map<String, Object> result = new HashMap<String, Object>();
        if (ToolsUtil.isEmpty(loginUser)) {
            result.put("msg", "未登录！");
            return result;
        }
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        String dataSourceName = params.get("sysEname");
        if (ToolsUtil.isEmpty(dataSourceName)) {
            result.put("msg", "系统标识不能可为空！");
            return result;
        }
        log.info("检查数据源连接是否正常："+dataSourceName);
        Connection connection = null;
        try {
            connection = ConnctionData.getInstance().getConnection(dataSourceName);
            result.put("flag", "T");
            result.put("msg", "连接成功！");
        } catch (SQLException e) {
            result.put("flag", "F");
            result.put("msg", e.getMessage());
            e.printStackTrace();
        }finally {
            ConnctionData.getInstance().close(connection,null,null);
        }
        return result;
    }

/**
 * ******************************************以下为以前老方法**************************************
 */


    /**
     * 机构保存
     */
    @RequestMapping(value = "/trans/submit/orgSave", method = RequestMethod.POST)
    @ResponseBody
    public Object orgSave(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        Map<String, Object> result = new HashMap<String, Object>();
        if (ToolsUtil.isEmpty(loginUser)) {
            result.put("msg", "未登录！");
            return result;
        }
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        String fatherId = params.get("fatherId");
        String fullEname="",fullCname="",subCount="0";
        paramsMap.putAll(params);
        String sysEname = StrUtil.doNull(params.get("sysEname"),CommonUtils.getConfigVal("sysEname"));
        paramsMap.put("sysEname", sysEname);
        if("0".equals(fatherId)){
            fullEname="/"+params.get("orgEname");
            fullCname="/"+params.get("orgCname");
        }
        paramsMap.put("fullEname", fullEname);
        paramsMap.put("fullCname", fullCname);
        paramsMap.put("subCount", subCount);
        paramsMap.put("createTime",Long.toString(System.currentTimeMillis()));
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "orgSave", paramsMap,ss);
        if(entity==null){
            result.put("flag", "F");
            result.put("msg", "操作失败");
        }else {
            if ("查询成功".equals(entity.getResult()) || "true".equals(entity.getResult()) || "T".equals(entity.getResult())) {
                result.put("flag", "T");
                result.put("msg", String.valueOf(entity.getEntity()));
            } else {
                result.put("flag", "F");
            }
        }
        return result;
    }
    /**
     * 机构删除
     */
    @RequestMapping(value = "/trans/submit/orgDel", method = RequestMethod.POST)
    @ResponseBody
    public Object orgDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        Map<String, Object> result = new HashMap<String, Object>();
        if (ToolsUtil.isEmpty(loginUser)) {
            result.put("msg", "未登录！");
            return result;
        }
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        paramsMap.put("infoType", "org");
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "orgDel", paramsMap,ss);
        result = super.getResultEntityData(entity);
        return result;
    }
    /**
     * 部门保存
     */
    @RequestMapping(value = "/trans/submit/deptSave", method = RequestMethod.POST)
    @ResponseBody
    public Object deptSave(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        Map<String, Object> result = new HashMap<String, Object>();
        if (ToolsUtil.isEmpty(loginUser)) {
            result.put("msg", "未登录！");
            return result;
        }
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        String sysEname = CommonUtils.getConfigVal("sysEname");
        paramsMap.put("sysEname", sysEname);
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "deptAction", paramsMap,ss);
        result = super.getResultEntityData(entity);
        return result;
    }
    /**
     * 部门删除
     */
    @RequestMapping(value = "/trans/submit/deptDel", method = RequestMethod.POST)
    @ResponseBody
    public Object deptDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        Map<String, Object> result = new HashMap<String, Object>();
        if (ToolsUtil.isEmpty(loginUser)) {
            result.put("msg", "未登录！");
            return result;
        }
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        String sysEname = CommonUtils.getConfigVal("sysEname");
        paramsMap.put("sysEname", sysEname);
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "deptAction", paramsMap,ss);
        result = super.getResultEntityData(entity);
        return result;
    }

    /**
     *查询角色列表
     */
    @RequestMapping(value = "/trans/list/actRoles", method = RequestMethod.GET)
    @ResponseBody
    public Object actRolesList(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("flag", "F");
        LoginUser user = PBOSSOTools.getLoginUser(request);
        String orgId = StrUtil.doNull(request.getParameter("orgId"), user.getOrgId());
        if (StrUtil.isNull(orgId)) {
            resultMap.put("msg", "缺少必要的方法参数！");
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgId", orgId);//必要参数
        paramMap.put("sysUser", user.getSysFlag());
        paramMap.put("userOrgId", user.getOrgId());
        paramMap.put("sysPass", ConfigUtil.getInstance().interfacePass());
        ResultEntity entity = UctCoreClient.searchRoleByReflect(paramMap);

        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            getListData(entity, resultMap);
        }
        return resultMap;
    }

    /**
     * 查询机构列表
     */
    @RequestMapping(value = "/trans/list/orgList", method = RequestMethod.GET)
    @ResponseBody
    public Object orgList(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = getParameterMap(request);
        Map<String, Object> data = new HashMap<String, Object>();
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        if (ToolsUtil.isEmpty(loginUser)) {
            data.put("msg", "未登录！");
            return data;
        }
        params.put("pageIndex", StrUtil.doNull(params.get("page"), params.get("pageIndex")));
        params.put("pageSize", StrUtil.doNull(params.get("rows"), params.get("pageSize")));

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        paramsMap.put("userId", loginUser.getUserId());
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "orgList", paramsMap,ss);
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            getListData(entity, data);
        }
        return data;
    }

    /**
     * 查询部门列表
     */
    @RequestMapping(value = "/trans/list/deptList", method = RequestMethod.GET)
    @ResponseBody
    public Object deptList(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = getParameterMap(request);
        Map<String, Object> data = new HashMap<String, Object>();
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        if (ToolsUtil.isEmpty(loginUser)) {
            data.put("msg", "未登录！");
            return data;
        }
        params.put("pageIndex", StrUtil.doNull(params.get("page"), params.get("pageIndex")));
        params.put("pageSize", StrUtil.doNull(params.get("rows"), params.get("pageSize")));

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        if (Validator.isNotNull(String.valueOf(params.get("orgId")))) {
            paramsMap.put("orgId", String.valueOf(params.get("orgId")));
        }
        String fatherId = Validator.getDefaultStr(params.get("fatherId"), "0");
        paramsMap.put("fatherId", fatherId);
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "deptList", paramsMap,ss);
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            getListData(entity, data);
        }
        return data;
    }
    /**
     * 查询角色列表
     */
    @RequestMapping(value = "/trans/list/roleList", method = RequestMethod.GET)
    @ResponseBody
    public Object roleList(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = getParameterMap(request);
        Map<String, Object> data = new HashMap<String, Object>();
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        if (ToolsUtil.isEmpty(loginUser)) {
            data.put("msg", "未登录！");
            return data;
        }
        params.put("pageIndex", StrUtil.doNull(params.get("page"), params.get("pageIndex")));
        params.put("pageSize", StrUtil.doNull(params.get("rows"), params.get("pageSize")));

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        paramsMap.put("sysUser", loginUser.getSysFlag());
        paramsMap.put("userOrgId", loginUser.getOrgId());
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "roleList", paramsMap,ss);
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            getListData(entity, data);
        }
        return data;
    }

    /**
     * 用户信息
     */
    @RequestMapping(value = "/trans/info/userInfo", method = RequestMethod.GET)
    @ResponseBody
    public Object userInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = getParameterMap(request);
        String username = StrUtil.doNull(params.get("userEname"), "");
        String userId = StrUtil.doNull(params.get("userId"), "");
        ResultEntity entity = null;
        JSONObject array =null;
        if (StrUtil.isNull(userId)) {
            entity = UctCoreClient.getUserByProperty("userEname", username);
        } else {
            entity = UctCoreClient.getUserByProperty("userId", userId);
        }
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            if (("true".equals(entity.getResult())) && !ToolsUtil.isEmpty(entity.getEntity())) {
                array = JSONObject.fromObject(entity.getEntity());
            }
        }
        return array;
    }

    /**
     * 机构信息
     */
    @RequestMapping(value = "/trans/info/orgInfo", method = RequestMethod.GET)
    @ResponseBody
    public Object orgInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, String> params = getParameterMap(request);
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        if (ToolsUtil.isEmpty(loginUser)) {
            data.put("msg", "未登录！");
            return data;
        }
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        if (Validator.isNotNull(String.valueOf(params.get("orgId")))) {
            paramsMap.put("propertyName", "orgId");
            paramsMap.put("propertyValue", String.valueOf(params.get("orgId")));
        }
        if (Validator.isNotNull(String.valueOf(params.get("orgEname")))) {
            paramsMap.put("propertyName", "orgEname");
            paramsMap.put("propertyValue", String.valueOf(params.get("orgEname")));
        }
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "orgEntity", paramsMap,ss);
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        }
        JSONObject array =null;
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            if (entity.getEntity() instanceof String) {
                ResultMsg msg = new ResultMsg("F", entity.getMessage());
                array = JSONObject.fromObject(msg);
            } else {
                array = JSONObject.fromObject(entity.getEntity());
            }
        }
        return array;
    }

    /**
     * 查询机构权限数据
     */
    @RequestMapping(value = "/trans/list/orgActData", method = RequestMethod.GET)
    @ResponseBody
    public Object orgActData(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("flag", "F");

        Map<String, String> params = getParameterMap(request);
        LoginUser loginUser = BeanFactory.getInstance().getLoginUser(request);
        if (ToolsUtil.isEmpty(loginUser)) {
            data.put("msg", "未登录！");
            return data;
        }

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        paramsMap.put("method", "orgActData");
        paramsMap.put("nowOrgId", loginUser.getOrgId());
        paramsMap.put("nowSysUser", loginUser.getSysFlag());
        paramsMap.put("ParamType", "json");

        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "orgAction", paramsMap,ss);

        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            getListData(entity, data);
        }
        return data;
    }



    /**
     *查询权限用户列表
     */
    @RequestMapping(value = "/trans/list/actUsers", method = RequestMethod.GET)
    @ResponseBody
    public Object actUsersList(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("flag", "F");
        LoginUser user = PBOSSOTools.getLoginUser(request);
        String deptId = StrUtil.doNull(request.getParameter("deptId"), user.getDeptId());
        if ((StrUtil.isNull(request.getParameter("roleId")) && StrUtil
                .isNull(request.getParameter("actId"))) || StrUtil.isNull(deptId)) {
            data.put("msg", "缺少必要的方法参数！");
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (StrUtil.isNotNull(request.getParameter("roleId")))
            paramMap.put("roleId", request.getParameter("roleId"));//与actId两选一
        if (StrUtil.isNotNull(request.getParameter("actId")))
            paramMap.put("actId", request.getParameter("actId"));
        paramMap.put("deptcross",
                StrUtil.doNull(request.getParameter("deptcross"), "1"));//1:部门内,2:跨部门,3:机构内,4:跨机构
        paramMap.put("deptId", deptId);//必要参数
        paramMap.put("sysPass", ConfigUtil.getInstance().interfacePass());
        paramMap.put("methodType", "getUsersStr");
        ResultEntity entity = UctProtoclClient.getInstance()
                .doService("uctService", "flowCore", paramMap);

        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            getListData(entity, data);
        }
        return data;
    }

    //////////////////以下对日志的处理doCode:logExecute////////////////////
    //启用、停用日志监控表
    @RequestMapping(value = "/trans/info/logExecute", method = RequestMethod.GET)
    @ResponseBody
    public Object logExecute(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("flag","F");

        Map<String, String> params = this.getParameterMap(request);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);

        String methodKey = "logExecute";
        if ("modelDetailList".equalsIgnoreCase(params.get("exeType"))) {
            methodKey = "logModelDetailList";
        }
        InterfaceRegister interfaceRegister =  UctProtoclClient.getInstance().getInterfaceRegister("audit", "audit_server_url", "audit_server_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("auditService", methodKey, paramsMap,interfaceRegister);


        if (Validator.isEmpty(entity.getEntity())) {
            data.put("flag","F");
            data.put("msg",entity.getMessage());
        } else {
            data.put("flag","T");
            data.put("msg",String.valueOf(entity.getEntity()));
        }

        return data;
    }

    //日志流水查询
    @RequestMapping(value = "/trans/list/logOperationList", method = RequestMethod.GET)
    @ResponseBody
    public Object logOperationList(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = getParameterMap(request);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        InterfaceRegister interfaceRegister =  UctProtoclClient.getInstance().getInterfaceRegister("audit", "audit_server_url", "audit_server_port");

        ResultEntity entity =  LoadRpcService.service()
                .doServiceClient("auditService", "logOperationList", paramsMap,
                        interfaceRegister);
        Map<String, Object> data = new HashMap<String, Object>();
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            getListData(entity, data);
        }
        return data;
    }

    //日志流水详情查看
    @RequestMapping(value = "/trans/info/viewLogDetail", method = RequestMethod.GET)
    @ResponseBody
    public Object viewLogDetail(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        InterfaceRegister interfaceRegister =  UctProtoclClient.getInstance().getInterfaceRegister("audit", "audit_server_url", "audit_server_port");
        ResultEntity entity =  LoadRpcService.service()
                .doServiceClient("auditService", "viewLogDetail", paramsMap,
                        interfaceRegister);

        Map<String, Object> data = new HashMap<String, Object>();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            Map map = (Map) entity.getEntity();
            data.put("item", map.get("item"));
            data.put("total", map.get("total"));
            data.put("rows", map.get("rows"));
        }
        return data;
    }

    @RequestMapping( value = "/trans/list/userList", method = RequestMethod.GET )
    public Object userList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        this.log.info(params);
//        LoginUser loginUser = PBOSSOTools.getLoginUser(request);
        /*if (!"1".equalsIgnoreCase(loginUser.getSysFlag())) {
            String orgId;
            if ("0".equalsIgnoreCase(loginUser.getSysFlag())) {
                orgId = StrUtil.doNull(loginUser.getDeptId(), "");
                params.put("dataDeptIds", StrUtil.doNull(loginUser.getDataDeptIds(), orgId));
            }

            orgId = StrUtil.doNull(loginUser.getOrgId(), "");
            params.put("dataOrgIds", StrUtil.doNull(loginUser.getDataOrgIds(), orgId));
        }
*/

//        ResultEntity entity = (ResultEntity)this.uctPlugin.doInvoke(request, response, params);
        Map<String, Object> paramsMap = new HashMap<String, Object>();

        paramsMap.putAll(params);
        InterfaceRegister ss =  UctProtoclClient.getInstance().getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        ResultEntity entity =  LoadRpcService.service().doServiceClient("uctService", "userList", paramsMap,ss);

        Map<String, Object> data = new HashMap();
        if (null == entity.getEntity()) {
            this.log.info(entity.getMessage());
        } else {
            this.getListData(entity, data);
        }

        return data;
    }

}
